package main
import(
	"fmt"
    "nightowl/download"    
    . "nightowl/model"
    "time"
    "io/ioutil"
    "gopkg.in/yaml.v2"
    "regexp"
)


var RE_VAR_PATTERN = regexp.MustCompile(`(\$\{?((\w+\.)?\w+(\[\d\])?)\}?)`)
func test_yaml(){
    var delivery Delivery
    yamlFile, err := ioutil.ReadFile("delivery.yaml")
    if err != nil {
        fmt.Println("error: %v", err)
        return
    }
    if err := yaml.Unmarshal(yamlFile, &delivery); err != nil {
        fmt.Println("error: %v", err)
    } 
    // 打印字段的类型
    fmt.Println("val:", delivery)

     // 将结构体转换为YAML
    y, err := yaml.Marshal(&delivery)
    if err != nil {
        fmt.Printf("=====Marshal error: %v", err)
    }
    if y != nil {
         fmt.Printf("finished:", string(y))
    }
}
func test_regexp(){
    s := "A:${aaa}, B:$bb10, C:${aaa.ccc1[0]}, $ccc2[0]"
    m := RE_VAR_PATTERN.FindAllStringSubmatch(s, -1)
    fmt.Printf("%v\n",m)
    for _, items := range m{
        fmt.Printf("=======%v===========\n", items)
        for j, item := range items{
            fmt.Printf("[%d]: %s\n", j, item)
        }
    }
}
func main(){
    test_regexp()
    // test_yaml()
    // item := &download.DownloadItem{
    //     Url: "aaa",
    //     DestPath: "yyy",
    //     Msg: "",
    // }
    // var items []*download.DownloadItem
    // items = append(items, item)
    // item.Url="xxx"
    // for i, x := range items{
    //     fmt.Println("===",i, x.Url)
    // } 
    // if true{return}

    // queue := make(chan string, 2)
    // go func(){queue <- "one"}()
    // go func(){queue <- "two"}()
    // // 
    // close(queue)
    // fmt.Println(<-queue)
    // fmt.Println(<-queue)
    // for elem := range queue {
    //     fmt.Println("xxx:", elem)
    // }
}
func testa_download(){


    var numbers []int
    numbers = append(numbers, 11)
    fmt.Println("len:", len(numbers), " cap:", cap(numbers))
    basicAuth := map[string]string{ "username": "p-lFG2qUTwRn", "password": "j8160YiYP2h9", }
    downloader := download.NewDownloadManager(3, func(d *download.DownloadManager, item *download.DownloadItem){
        fmt.Printf( "item.Url:%s|msg:%s\n", item.Url, item.Msg)
    },func(d *download.DownloadManager){
        fmt.Printf( "Finished...\n")
    })
    // fmt.Printf("TryLock2: %t\n", downloader.Mutex.TryLock()) 
    // fmt.Printf("TryLock3: %t\n", downloader.Mutex.TryLock()) 
    downloader.Enqueue("1", "https://nexus.dep.devops.cmit.cloud:20030/repository/auto_deploy_files/devops-middleware/gitlab-20230703.tar.gz", basicAuth, "D:/docs/gitlab-20230703.tar.gz")
    // sha256: e2ec98ee3bbabf58f30170aa426a3181cba3c0d6f8882d625c5bd6d2b858afe5
    // md5: f6b92ff4f5b4a9aa9262e4b22c64d52a
    downloader.Enqueue("2", "https://nexus.dep.devops.cmit.cloud:20030/repository/auto_deploy_files/k8s-binaries-amd64/calico-crd-v3.26.4.tar.gz", basicAuth, "D:/docs/calico-crd-v3.26.4.tar.gz")
    // sha256: 481e52de684c049f3f7f7bac78f0f6f4ae424d643451adc9e3d3fa9d03fb6d57
    // md5: 40b6a6743dfad291918710abd51410d2
    downloader.Enqueue("3", "https://nexus.dep.devops.cmit.cloud:20030/repository/auto_deploy_files/k8s-binaries-arm64/kubernetes-node-linux-arm64.tar.gz", basicAuth,  "D:/docs/kubernetes-node-linux-arm64.tar.gz")
    // sha256: d3f116b375be0058b560ff1802d7fec4da6dac684ec8649a75718c1fc07b6a6f
    // md5: 73bc4dfc1a1ca9e1da21712733db9f60
    time.Sleep(200 * time.Second)
}